package cn.ld.modules.demo.test.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.ld.common.api.vo.Result;
import cn.ld.common.system.query.QueryGenerator;
import cn.ld.modules.demo.test.entity.LdOrderCustomer;
import cn.ld.modules.demo.test.entity.LdOrderMain;
import cn.ld.modules.demo.test.entity.LdOrderTicket;
import cn.ld.modules.demo.test.service.ILdOrderCustomerService;
import cn.ld.modules.demo.test.service.ILdOrderMainService;
import cn.ld.modules.demo.test.service.ILdOrderTicketService;
import cn.ld.modules.demo.test.vo.LdOrderMainPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Description: 一对多示例（ERP TAB风格）
 * @Author: ZhiLin
 * @Date: 2019-02-20
 * @Version: v2.0
 */
@Slf4j
@RestController
@RequestMapping("/test/order")
public class LdOrderErpMainController {

    @Autowired
    private ILdOrderMainService ldOrderMainService;
    @Autowired
    private ILdOrderCustomerService ldOrderCustomerService;
    @Autowired
    private ILdOrderTicketService ldOrderTicketService;

    /**
     * 分页列表查询
     *
     * @param ldOrderMain
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/orderList")
    public Result<?> respondePagedData(LdOrderMain ldOrderMain,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {
        QueryWrapper<LdOrderMain> queryWrapper = QueryGenerator.initQueryWrapper(ldOrderMain, req.getParameterMap());
        Page<LdOrderMain> page = new Page<LdOrderMain>(pageNo, pageSize);
        IPage<LdOrderMain> pageList = ldOrderMainService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param ldOrderMainPage
     * @return
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody LdOrderMainPage ldOrderMainPage) {
        LdOrderMain ldOrderMain = new LdOrderMain();
        BeanUtils.copyProperties(ldOrderMainPage, ldOrderMain);
        ldOrderMainService.save(ldOrderMain);
        return Result.ok("添加成功!");
    }

    /**
     * 编辑
     *
     * @param ldOrderMainPage
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<?> edit(@RequestBody LdOrderMainPage ldOrderMainPage) {
        LdOrderMain ldOrderMain = new LdOrderMain();
        BeanUtils.copyProperties(ldOrderMainPage, ldOrderMain);
        ldOrderMainService.updateById(ldOrderMain);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        ldOrderMainService.delMain(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ldOrderMainService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        LdOrderMain ldOrderMain = ldOrderMainService.getById(id);
        return Result.ok(ldOrderMain);
    }


    /**
     * 通过id查询
     *
     * @param ldOrderCustomer
     * @return
     */
    @GetMapping(value = "/listOrderCustomerByMainId")
    public Result<?> queryOrderCustomerListByMainId(LdOrderCustomer ldOrderCustomer,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<LdOrderCustomer> queryWrapper = QueryGenerator.initQueryWrapper(ldOrderCustomer, req.getParameterMap());
        Page<LdOrderCustomer> page = new Page<LdOrderCustomer>(pageNo, pageSize);
        IPage<LdOrderCustomer> pageList = ldOrderCustomerService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 通过id查询
     *
     * @param ldOrderTicket
     * @return
     */
    @GetMapping(value = "/listOrderTicketByMainId")
    public Result<?> queryOrderTicketListByMainId(LdOrderTicket ldOrderTicket,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<LdOrderTicket> queryWrapper = QueryGenerator.initQueryWrapper(ldOrderTicket, req.getParameterMap());
        Page<LdOrderTicket> page = new Page<LdOrderTicket>(pageNo, pageSize);
        IPage<LdOrderTicket> pageList = ldOrderTicketService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param ldOrderCustomer
     * @return
     */
    @PostMapping(value = "/addCustomer")
    public Result<?> addCustomer(@RequestBody LdOrderCustomer ldOrderCustomer) {
        ldOrderCustomerService.save(ldOrderCustomer);
        return Result.ok("添加成功!");
    }

    /**
     * 编辑
     *
     * @param ldOrderCustomer
     * @return
     */
    @RequestMapping(value = "/editCustomer", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<?> editCustomer(@RequestBody LdOrderCustomer ldOrderCustomer) {
        ldOrderCustomerService.updateById(ldOrderCustomer);
        return Result.ok("添加成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteCustomer")
    public Result<?> deleteCustomer(@RequestParam(name = "id", required = true) String id) {
        ldOrderCustomerService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchCustomer")
    public Result<?> deleteBatchCustomer(@RequestParam(name = "ids", required = true) String ids) {
        this.ldOrderCustomerService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 添加
     *
     * @param ldOrderTicket
     * @return
     */
    @PostMapping(value = "/addTicket")
    public Result<?> addTicket(@RequestBody LdOrderTicket ldOrderTicket) {
        ldOrderTicketService.save(ldOrderTicket);
        return Result.ok("添加成功!");
    }

    /**
     * 编辑
     *
     * @param ldOrderTicket
     * @return
     */
    @RequestMapping(value = "/editTicket", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<?> editTicket(@RequestBody LdOrderTicket ldOrderTicket) {
        ldOrderTicketService.updateById(ldOrderTicket);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteTicket")
    public Result<?> deleteTicket(@RequestParam(name = "id", required = true) String id) {
        ldOrderTicketService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchTicket")
    public Result<?> deleteBatchTicket(@RequestParam(name = "ids", required = true) String ids) {
        this.ldOrderTicketService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

}